package com.sduept.nwld.dataserver.manager.protection;

import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.model.daily.Runlog;
import com.sduept.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RunlogManager {
	@Autowired
	SuperDAO dao;

	/**
	 * 获取所有调度日志类型
	 * @return List<String> types
	 */
	public List<String> getRunlogTypes() {
		List<Runlog> log = dao.findByCondition(Runlog.class, "1=1 group by log_type");
		List<String> types = new ArrayList<>();
		for (Runlog l : log) {
			types.add(l.getLog_type());
		}
		return types;
	}

	/**
	 * 根据记录时间获取调度日志数据
	 * @param start (Strig)
	 * @param end (String)
	 * @return List<Runlog>
	 */
	public List<Runlog> getRunlog(String start, String end) {
		Date s = DateUtil.getDateByString(start, DateUtil.pattern_ymdhms);
		Date e = DateUtil.getDateByString(end, DateUtil.pattern_ymdhms);
		return dao.findByCondition(Runlog.class, " time between ? and ? order by time DESC", s, e);
	}

	/**
	 * 根据记录时间获取调度日志数据
	 * @param start (Date)
	 * @param end (Date)
	 * @return List<Runlog>
	 */
	public List<Runlog> getRunlogByTime(Date start, Date end) {
		return dao.findByCondition(Runlog.class, " time between ? and ? order by time DESC", start, end);
	}

	/**
	 * 根据记录时间获取调度日志数据--每次返回限定容量
	 * @param start
	 * @param end
	 * @param size
	 * @return  List<Runlog>
	 */
	public List<Runlog> getRunlog(String start, String end, int size) {
		Date s = DateUtil.getDateByString(start, DateUtil.pattern_ymdhms);
		Date e = DateUtil.getDateByString(end, DateUtil.pattern_ymdhms);
		String whereClause = "time between ? and ?";
		Object[] whereArgs = new Object[] { s, e };
		int[] rangeArr = null;
		if (size > 9) {
			rangeArr = new int[] { size - 10, size };
		} else if (size > 0) {
			rangeArr = new int[] { 0, size };
		}
		return dao.findByCondition(whereClause, whereArgs, "order by time DESC", rangeArr, Runlog.class);
	}

	public List<Runlog> getRunLogByDateAndType(String start, String end, String type) {
		Date s = DateUtil.getDateByString(start, DateUtil.pattern_ymdhms);
		Date e = DateUtil.getDateByString(end, DateUtil.pattern_ymdhms);
		return dao.findByCondition(Runlog.class, " time between ? and ? and log_type = ? order by time DESC",
				s, e, type);
	}

	public List<Runlog> getRunLogByDateAndType(String start, String end, String type, int size) {
		Date s = DateUtil.getDateByString(start, DateUtil.pattern_ymdhms);
		Date e = DateUtil.getDateByString(end, DateUtil.pattern_ymdhms);
		String whereClause = "time between ? and ? and log_type = ?";
		Object[] whereArgs = new Object[] { s, e, type };
		int[] rangeArr = null;
		if (size > 9) {
			rangeArr = new int[] { size - 10, size };
		} else if (size > 0) {
			rangeArr = new int[] { 0, size };
		}
		return dao.findByCondition(whereClause, whereArgs, "order by time DESC", rangeArr, Runlog.class);
	}

	public List<Runlog> getRunlogByCondition(Date start, Date end, String likeSth, String selectedType) {
		String sql = "1=1 ";
		List<Object> params = new ArrayList<>();
		if (StringUtils.isNotEmpty(selectedType)) {
			sql += "and log_type = ? ";
			params.add(selectedType);
		}
		if (StringUtils.isNotEmpty(likeSth)) {
			likeSth = "%" + likeSth + "%";
			sql += "and ( recorder like ? or log_contents like ? or log_type like ? )";
			params.add(likeSth);
			params.add(likeSth);
			params.add(likeSth);
		}
		sql += " and time between ? and ? order by time DESC";
		params.add(start);
		params.add(end);
		return dao.findByCondition(sql, params.toArray(), Runlog.class);
	}
	public List<Runlog> getRunlogByCondition(Date start, Date end, String likeSth, String selectedType,int size) {
		String sql = "1=1 ";
		List<Object> params = new ArrayList<>();
		if (StringUtils.isNotEmpty(selectedType)) {
			sql += "and log_type = ? ";
			params.add(selectedType);
		}
		if (StringUtils.isNotEmpty(likeSth)) {
			likeSth = "%" + likeSth + "%";
			sql += "and ( recorder like ? or log_contents like ? or log_type like ? )";
			params.add(likeSth);
			params.add(likeSth);
			params.add(likeSth);
		}
		sql += " and time between ? and ? ";
		params.add(start);
		params.add(end);
		int[] rangeArr = null;
		if (size > 9) {
			rangeArr = new int[] { size - 10, size };
		} else if (size > 0) {
			rangeArr = new int[] { 0, size };
		}
		return dao.findByCondition(sql, params.toArray(), "order by time DESC", rangeArr, Runlog.class);
	}

	public List<Runlog> getAllRunlogs() {
		return dao.findAll(Runlog.class);
	}

	/**
	 * 单条编辑
	 */
	public void editUpdate(Runlog selected) {
		if (selected != null) {
			dao.update(selected, Runlog.class);
		}
	}

	/**
	 * 数据更新
	 */
	public void updateRunlog(Runlog selectLog) {
		if (selectLog != null) {
			try {
				dao.update(selectLog, Runlog.class); // update新的数据到这条Overhaulmag
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 新增数据
	 * 
	 * @return
	 */
	public Runlog creatRunlog(Runlog created) {
		if (created != null) {
			try {
				created = dao.create(created, Runlog.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return created;
	}

	/**
	 * 单条删除
	 */
	public void delRunlog(Runlog selectLog) {
		if (selectLog != null) {
			try {
				dao.delete(selectLog.getId(), Runlog.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
